package com.lhf.sparecost.service.impl;


import com.lhf.enumfile.Currency;
import com.lhf.enumfile.IsNot;
import com.lhf.enumfile.Status;
import com.lhf.error.ErrorMessageVo;
import com.lhf.planClass.model.PlanClassVo;
import com.lhf.planClass.model.StatusVo;
import com.lhf.sparecost.mapper.SpareCostMapper;
import com.lhf.sparecost.model.*;
import com.lhf.sparecost.service.SpareCostService;
import com.lhf.util.AllrecordsVo;
import com.lhf.util.DateUtil;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.*;

/**
 * @author 尐痞猫
 * @date2021-06-25 22:50
 */
@Service
public class SpareCostServiceImpl implements SpareCostService {



    @Autowired
    private SpareCostMapper spareCostMapper;


    //根据法人查询工厂下拉栏
    @Override
    public ResultVo queryFactorys(String legalPersonCode) {
        return ResultBuliderVo.success(spareCostMapper.queryFactorys(legalPersonCode));
    }
    //含税类型下拉栏
    @Override
    public ResultVo queryAllTaxType() {
        List<TaxTypeVo> list = spareCostMapper.queryAllTaxType();
        return ResultBuliderVo.success(list);
    }

    @Override
    public ResultVo getPullDownList() {
        List<StatusVo2> statusVos = new ArrayList<>();
        for (Status c : Status.values()) {
            statusVos.add(new StatusVo2(c.getStatusName(),c.getStatus()));
        }
        List<CurrencyVo> currencyCode = new ArrayList<>();
        for (Currency c: Currency.values()) {
            currencyCode.add(new CurrencyVo(c.getCurrencyName(),c.getCurrencyCode()));
        }
       List<PriceTypeVo> list = spareCostMapper.queryPriceType();
        Map<String,List> map = new HashMap<>();
        map.put("materialCostPriceStatus",statusVos);
        map.put("currencyCode",currencyCode);
        map.put("priceType",list);
        return ResultBuliderVo.success(map);
    }

    @Override
    public ResultVo queryPage(SpareCostSelectVo spareCostSelectVo) {
        List<SpareCostQueryVo> list = spareCostMapper.queryPage(spareCostSelectVo);
        for(SpareCostQueryVo spareCostQueryVo :list){
            spareCostQueryVo.setCurrencyName(Currency.getCurrencyName(spareCostQueryVo.getCurrencyCode()));
            spareCostQueryVo.setStatusName(Status.getStatusName(spareCostQueryVo.getStatusCode()));
        }
        int count = spareCostMapper.queryCount(spareCostSelectVo);
        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setPageIndex(spareCostSelectVo.getPageIndex());
        allrecordsVo.setPageSize(spareCostSelectVo.getPageSize());
        allrecordsVo.setTotalSize(count);
        allrecordsVo.resettotalNum(count);
        allrecordsVo.setDataList(list);
        return ResultBuliderVo.success(allrecordsVo);
    }

    @Override
    public ResultVo queryspareClass(SpareClassQueryVo spareClassQueryVo) {
        List<SpareClassVo> list =spareCostMapper.queryspareClass(spareClassQueryVo);
        int count = spareCostMapper.queryspareClassCount(spareClassQueryVo);
        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setPageIndex(spareClassQueryVo.getPageIndex());
        allrecordsVo.setPageSize(spareClassQueryVo.getPageSize());
        allrecordsVo.setTotalSize(count);
        allrecordsVo.resettotalNum(count);
        allrecordsVo.setDataList(list);
        return ResultBuliderVo.success(allrecordsVo);
    }

    @Override
    public ResultVo add(SpareCostAddVo spareCostAddVo) {
        //根据输入的工厂与备件，查询当前工厂是否有该备件
        int result = spareCostMapper.CheckBySpareCostCodeAndFactoryCode(spareCostAddVo.getSpareClassCode(),spareCostAddVo.getLegalPlantCode());
        if(result==0){
            return ResultBuliderVo.error(ErrorMessageVo.GAIGONGCHANGMEIYOUDUIYINGPEIJIANXINXI);
        }
        //如果输入的结束时间为空，新增当前日期的时间  格式为2021-07-06 23:59:59
        if(spareCostAddVo.getEndTime()==null){
            spareCostAddVo.setEndTime(DateUtil.getEndTime());
        }
        //根据新增数据开始时间 查询所有时间段结束时间比新增数据开始时间大的数据
        List<spareCostTimeVo>  list = spareCostMapper.querySpareCostByStartTime(spareCostAddVo);
        //根据spareCostAddVo
        spareCostAddVo.setStatusCode(spareCostAddVo.getStatusCode(spareCostAddVo.getStartTime(),spareCostAddVo.getEndTime()));
        SpareCostVo spareCostVo = new SpareCostVo();
        BeanUtils.copyProperties(spareCostAddVo,spareCostVo);
        spareCostVo.setCreateName("张三");
        spareCostVo.setCreateTime(DateUtil.NowDate());
        spareCostVo.setUpdateName("张三");
        spareCostVo.setUpdateTime(DateUtil.NowDate());
        //如果集合是空的执行添加方法
        if(list.isEmpty()){
            //新增前判断下当前时间段是否在启用期间
             spareCostMapper.add(spareCostVo);
        }else{
            for(spareCostTimeVo spareCostTimeVo:list){

                //如果新增开始时间比数据库已有开始时间要小或等于
                if(spareCostAddVo.getStartTime().getTime()<=spareCostTimeVo.getStartTime().getTime()){
                    //如果新增结束时间比数据库已有结束时间大
                    if(spareCostAddVo.getEndTime().getTime()>=spareCostTimeVo.getEndTime().getTime()){
                        //删除原有数据，新增新数据
                        spareCostMapper.delete(spareCostTimeVo.getId());
                    }
                    //如果新增结束时间比数据库已有结束时间小
                    if(spareCostAddVo.getEndTime().getTime()<spareCostTimeVo.getEndTime().getTime()){
                        SpareCostUpdateTimeVo spareCostUpdateTimeVo = new SpareCostUpdateTimeVo();
                        //将原有数据的开始时间变成新增数据结束时间+1天
                        spareCostUpdateTimeVo.setStartTime(DateUtil.date_addDay(spareCostAddVo.getEndTime()));
                        spareCostUpdateTimeVo.setId(spareCostTimeVo.getId());
                        spareCostMapper.updateTime(spareCostUpdateTimeVo);
                    }
                }
                //如果新增开始时间比数据库已有开始时间要大
                if(spareCostAddVo.getStartTime().getTime()>spareCostTimeVo.getStartTime().getTime()){
                    //如果新增结束时间比数据库已有结束时间大
                    if(spareCostAddVo.getEndTime().getTime()>=spareCostTimeVo.getEndTime().getTime()){
                        //将原有数据的结束时间变成新增数据开始时间-1天
                        SpareCostUpdateTimeVo spareCostUpdateTimeVo = new SpareCostUpdateTimeVo();
                        spareCostUpdateTimeVo.setEndTime(DateUtil.date_subDay(spareCostAddVo.getStartTime()));
                        spareCostUpdateTimeVo.setId(spareCostTimeVo.getId());
                        spareCostMapper.updateTime(spareCostUpdateTimeVo);
                        ;
                    }
                    //如果新增结束时间比数据库已有结束时间小
                    if(spareCostAddVo.getEndTime().getTime()<spareCostTimeVo.getEndTime().getTime()){
                        //将原有数据的结束时间变成新增数据开始时间-1天
                        SpareCostUpdateTimeVo spareCostUpdateTimeVo = new SpareCostUpdateTimeVo();
                        spareCostUpdateTimeVo.setEndTime(DateUtil.date_subDay(spareCostAddVo.getStartTime()));
                        spareCostUpdateTimeVo.setId(spareCostTimeVo.getId());
                        spareCostMapper.updateTime(spareCostUpdateTimeVo);
                        //并新增一个开始时间为新增开始时间+1天，结束时间为原有数据的结束时间
                        SpareCostVo spareCostVo1 = new SpareCostVo();
                        BeanUtils.copyProperties(spareCostTimeVo,spareCostVo1);
                        spareCostVo1.setStartTime(DateUtil.date_addDay(spareCostAddVo.getEndTime()));
                        spareCostVo1.setCreateName("张三");
                        spareCostVo1.setCreateTime(DateUtil.NowDate());
                        spareCostVo1.setUpdateName("张三");
                        spareCostVo1.setUpdateTime(DateUtil.NowDate());
                        spareCostMapper.add(spareCostVo1);

                    }
                }
            }
            //新增新数据
            spareCostMapper.add(spareCostVo);
        }
        return ResultBuliderVo.success(null);
    }
}
